<template>
  <el-dialog
    title="提示"
    :visible.sync="showdialogVisible"
    destroy-on-close
    @close="hidden"
  >
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @blur="manager">
          <el-option v-for="item in staff" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="hidden">取 消</el-button>
      <el-button type="primary" @click="postForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDepartment, postDepartment, staff } from '@/api/department'
export default {
  props: {
    showdialogVisible: {
      type: Boolean,
      require: true,
      default: false
    },
    nodeData: {
      type: Object,
      require: true,
      default: null
    }
  },
  data() {
    const name = (rule, value, callback) => {
      if (this.nodeData.children) {
        const name = this.nodeData.children.filter((v) => v.name === value)
        if (name.length > 0) {
          return callback(new Error('部门名不能重复'))
        }
      }
      callback()
    }
    const code = async(rule, value, callback) => {
      const { depts } = await getDepartment()
      if (depts.some((v) => v.code === value)) {
        return callback(new Error('部门编码只能唯一'))
      }
      callback()
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      staff: [],
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }, { validator: name, trigger: 'blur' }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }, { validator: code, trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  async created() {
    const res = await staff()
    this.staff = res
  },
  methods: {
    postForm() {
      this.$refs.form.validate((boolean, object) => {
        if (boolean) {
          postDepartment({ ...this.formData, pid: this.nodeData.id }).then((res) => {
            this.hidden()
            this.$emit('pullDepartment')
          })
        }
      })
    },
    hidden() {
      this.$refs.form.resetFields()
      this.$emit('hidden')
    },
    manager() {
      this.$refs.form.validateField('manager')
    },
    render(val) {
      this.formData = val
    }
  }
}
</script>

<style>

</style>
